﻿using System;
using System.Collections.Generic;
using System.Configuration;
using System.Linq;
using System.Text;
using UtilsLib.UtilsExtensions.String;

namespace UtilsLib.UtilsConfiguration
{
    public static class ConfigManager
    {
        /// <summary>
        /// Return the AppSettings Value linked to the key
        /// </summary>
        /// <param name="key"></param>
        /// <returns></returns>
        public static string AppSettings(string key)
        {
            return !ConfigurationManager.AppSettings[key].INOE() ?
                        ConfigurationManager.AppSettings[key].ToString() : null;
        }

        /// <summary>
        /// Returns the first ConnectionStrings found.
        /// </summary>
        /// <returns></returns>
        public static string ConnectionString()
        {
            return (ConfigurationManager.ConnectionStrings.Count > 0 ?
                        ConfigurationManager.ConnectionStrings[0].ConnectionString : "");
        }
        /// <summary>
        /// Returns the ConnectionString corresponding to the key
        /// </summary>
        /// <param name="key"></param>
        /// <returns></returns>
        public static string ConnectionString(string key)
        {
            return (ConfigurationManager.ConnectionStrings[key] != null) ?
                        ConfigurationManager.ConnectionStrings[key].ConnectionString : "";
        }
        public static bool HasKey(string key) { return ConfigurationManager.AppSettings.AllKeys.Contains(key); }

        /// <summary>
        /// Set a value to a app settings key in config files
        /// </summary>
        /// <param name="key">key of the setting</param>
        /// <param name="value">value of the key to set</param>
        public static void SetAppSetting(string key, string value)
        {
            ConfigurationManager.AppSettings[key] = value;
        }
    }
}
